package com.zimi.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zimi.model.SysRole;
import com.zimi.service.SysRoleService;
import com.zimi.util.GsonUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统角色表 api
 *
 * @author lnintr
 * @since 2019-05-16
 */
@RestController
@RequestMapping("sys-role")
public class SysRoleApi {

    @Autowired
    private SysRoleService sysRoleService;

    @PostMapping("list")
    @ApiOperation("查询角色一览信息")
    public Object list(SysRole role, int page, int limit) {
        Page result = (Page) sysRoleService.list(role, page, limit);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("data", result.getRecords());
        map.put("count", result.getTotal());
        return map;
    }

    @PostMapping("allList")
    @ApiOperation("查询角色一览信息")
    public Object allList() {
        Map map = new HashMap();
        map.put("code", 0);
        map.put("List", sysRoleService.allList());
        return map;
    }

    @PostMapping("add")
    @ApiOperation("添加角色信息")
    public Object add(SysRole role) {
        sysRoleService.insert(role);
        Map map = new HashMap();
        map.put("code", 0);
        return map;
    }

    @PostMapping("update")
    @ApiOperation("修改角色信息")
    public Object upd(SysRole role) {
        sysRoleService.update(role);
        Map map = new HashMap();
        map.put("code", 0);
        return map;
    }

    @PostMapping("delete")
    @ApiOperation("删除角色信息")
    public Object del(SysRole role) {
        role.setRoleState(2);
        sysRoleService.update(role);
        Map map = new HashMap();
        map.put("code", 0);
        return map;
    }

    @PostMapping("move")
    @ApiOperation("移动角色顺序")
    public Object up(HttpServletRequest request) {
        String[] ma = request.getParameterValues("roleList");

        List<SysRole> sysRoleList = GsonUtil.json2Objs(ma[0], SysRole.class);
        Map map = new HashMap();
        map.put("code", sysRoleService.move(sysRoleList));
        return map;
    }

    @PostMapping("recycle")
    @ApiOperation("恢复机构类型")
    public Object del(HttpServletRequest request) {
        String[] ma = request.getParameterValues("roleList");

        List<SysRole> sysOrgCateList = GsonUtil.json2Objs(ma[0], SysRole.class);
        Map map = new HashMap();
        map.put("code", sysRoleService.recycle(sysOrgCateList));
        return map;
    }
}
